# https://www.cnblogs.com/huchong/p/8244279.html


# 装饰器模式方法
def Singleton(cls):
    _instance = {}

    def wwww222(*args, **kargs):
        # 该没有保存这个实例的时候，cls产生一个新的实例。并保存。线程不安全的。
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    # 该装饰器类返回一个装饰处理函数引用
    return wwww222


@Singleton
class A(object):
    a = 1

    def __init__(self, x=0):
        self.x = x


class Singleton2:
    def __new__(cls, *args, **kwargs):
        if hasattr(cls, 'instance'):
            return cls.instance
        else:
            cls.instance = super(Singleton2, cls).__new__(cls, *args, **kwargs)
            return cls.instance

    def __call__(self, *args, **kwargs):
        print("-----------Singleton2()被执行*args:%s**kwargs:%s-------------" % (args, kwargs))


print(Singleton2())
print(Singleton2())

# 实例() 则调用方法__call__。一般用来实例的内容
Singleton2()('zhang')


a1 = A(2)
a2 = A(3)
